package UILayer;

import java.awt.BorderLayout;
import java.awt.Point;

import CtrLayer.OrderCtr;
import ModelLayer.CustomerCollection;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
import javax.swing.BoxLayout;
import javax.swing.JRadioButton;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class OrderUI extends JInternalFrame
{
	private OrderCtr order_ctr;
	private boolean show_finished = false;
	private  JTable order_table;
	private String[] column_names = {"Sale number", "Customer name", "Delivery", "Total cost" };
	private JTextField search_tf;
	private JPanel panel_1;
	private JButton complete_btn;


	/**
	 * Create the frame.
	 */
	public OrderUI()
	{
		super("Manage sales", true, true, true);
		setBounds(180, 100, 513, 348);
		order_ctr = new OrderCtr();
		panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			public void internalFrameActivated(InternalFrameEvent e)
			{
				refresh();
			}
		});
		make_search();
		make_ctr_btns();
		make_table();
		make_radio_btns();

	}
	
	
	private void make_search()
	{
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.WEST);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel search_lbl = new JLabel("Sale number");
		panel_2.add(search_lbl);

		search_tf = new JTextField();
		panel_2.add(search_tf);
		search_tf.setColumns(10);

		JButton search_btn = new JButton("Search");
		search_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				search();
			}
		});
		panel_2.add(search_btn);
		
		JButton clear_btn = new JButton("Clear");
		clear_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				refresh();
				search_tf.setText("");
			}
		});
		panel_2.add(clear_btn);

	}
	private void make_table()
	{

		final Object[][] filling = CustomerCollection.give_all_unpaid_order_data();

		order_table = new JTable();

		order_table.setFillsViewportHeight(true);
		order_table.setModel(new MyTableModel(filling, column_names));

		order_table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		order_table.setAutoCreateRowSorter(true);
		order_table.setRowSelectionAllowed(true);
		order_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		order_table.getColumnModel().getColumn(0).setPreferredWidth(80);
		order_table.getColumnModel().getColumn(1).setPreferredWidth(120);
		order_table.getTableHeader().setReorderingAllowed(false);
		order_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		order_table.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					details();
				}
			}
		});
		
		

		DefaultTableCellRenderer right_renderer = new DefaultTableCellRenderer();
		right_renderer.setHorizontalAlignment(JLabel.RIGHT);
		order_table.setDefaultRenderer(String.class, right_renderer);

		JScrollPane scroll_pane = new JScrollPane(order_table);
		scroll_pane.setBorder(new EmptyBorder(5, 10, 5, 10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
	}
	
	private void make_radio_btns()
	{
		JPanel panel = new JPanel();
		panel_1.add(panel, BorderLayout.EAST);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		JRadioButton pending_rbtn = new JRadioButton("Pending orders");
		pending_rbtn.setSelected(true);
		pending_rbtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				show_finished = false;
				complete_btn.setEnabled(true);
				refresh();
			}
		});
		panel.add(pending_rbtn);
		
		JRadioButton finished_rdbtn = new JRadioButton("Finished orders");
		finished_rdbtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				show_finished = true;
				complete_btn.setEnabled(false);
				refresh();
			}
		});
		panel.add(finished_rdbtn);
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(pending_rbtn);
		bg.add(finished_rdbtn);
	}
	
	private void make_ctr_btns()
	{
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new BorderLayout(0, 0));

		JButton details_btn = new JButton("Details");
		details_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				details();
			}
		});
		panel.add(details_btn, BorderLayout.WEST);

		complete_btn = new JButton("Complete");
		complete_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				complete();
			}
		});
		panel.add(complete_btn, BorderLayout.EAST);
	}
	
	private void refresh()
	{
		Object[][] filling;
		if (show_finished)
		{
			filling = CustomerCollection.give_all_paid_order_data();
		}
		
		else
		{
			filling = CustomerCollection.give_all_unpaid_order_data();
		}

		order_table.setModel(new MyTableModel(filling, column_names));
		order_table.getColumnModel().getColumn(0).setPreferredWidth(80);
		order_table.getColumnModel().getColumn(1).setPreferredWidth(120);
	}
	private void search()
	{
		Object[][] filling = new Object[1][4];
		String contents = search_tf.getText();
		if (is_numeric(contents))
		{
			filling[0] = CustomerCollection.get_order_data(contents, show_finished);
			order_table.setModel(new MyTableModel(filling, column_names));
			order_table.getColumnModel().getColumn(0).setPreferredWidth(80);
			order_table.getColumnModel().getColumn(1).setPreferredWidth(120);
		}
		else
		{
			JOptionPane.showMessageDialog(
					this,
					"You must input a number.",
					"Error",
					JOptionPane.INFORMATION_MESSAGE);
		}
		
	}
	
	private void details()
	{
		int row_index = order_table.getSelectedRow();
		if (row_index != -1)
		{
			if(order_table.getModel().getValueAt(row_index, 0) != null)
			{
				OrderDetails od = new OrderDetails((String) order_table.getModel().getValueAt(row_index, 1), String.valueOf(order_table.getModel().getValueAt(row_index, 0)));
				getDesktopPane().add(od);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				od.setLocation(x, y);
				od.setVisible(true);
			}
		}
	}
	
	private void complete()
	{
		int row_index = order_table.getSelectedRow();
		if (row_index != -1)
		{
			if(order_table.getModel().getValueAt(row_index, 0) != null)
			{
				if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
						"Are you sure you complete this sale?",
						"Finish sale", JOptionPane.YES_NO_OPTION))
				{
					order_ctr.close_order(String.valueOf(order_table.getModel().getValueAt(row_index, 0)));
					refresh();
				}
			}
		}
	}
	
	private boolean is_numeric(String checker)
	{
		try
		{
			Integer.parseInt(checker);
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}

}
